# 该部分集成了mysql数据库的增删改接口，因为常用的是增和改，删和查接口没有加入flask接口列表中
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import pymysql
pymysql.install_as_MySQLdb()

app = Flask(__name__)
# 配置数据库的连接参数
#######################################cfg
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123456@localhost:3306/mydb'
#######################################
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

app.app_context().push()



class TCL(db.Model):
    __tablename__ = "test111"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255, 'utf8_general_ci'), info='姓名')
    sex = db.Column(db.String(4, 'utf8_general_ci'), info='性别')
    pid = db.Column(db.String(255), index=True, info='患者编号') 
    borntime = db.Column(db.DateTime, info='出生日期')
    hospital = db.Column(db.String(255), info='医院名')

    def __init__(self,name,sex,pid,hospital,borntime):
        self.sex = sex
        self.pid = pid
        self.name = name
        self.hospital = hospital
        self.borntime = borntime

# 增 方法
def sql_add():
    add_result = TCL(study_id="1351232322",inv_pred="0",men_pred="1")
    db.session.add(add_result)
    db.session.commit()
    
# 改 方法
def sql_update():    
    db.session.query(TCL).filter_by(study_id=1351232322).update({'inv_pred': "1"})
    db.session.commit()
  
# 删 方法  
def sql_delete():
    
    tcl = db.session.query(TCL).filter(TCL.study_id=="1351232322").first()
    db.session.delete(tcl)
    db.session.commit()


def TCL_update(study_id,inv_pred,men_pred):
    db.session.query(TCL).filter_by(study_id=study_id).update({'inv_pred': inv_pred,'men_pred':men_pred})
    db.session.commit()
    
def TCL_add(name,sex,pid,hospital,borntime):
    add_result = TCL(name=name,sex=sex,pid=pid,hospital=hospital,borntime=borntime)
    db.session.add(add_result)
    db.session.commit()

# db.create_all()
#TCL_add("李华","男","11","南方医院",'1997.10.1')
# {'PatientName': 'BAI_YUE_BIN', 'sex': 'M', 'borndate': '19710507', 'fid': 'F0630211', 'Study_id': 'MR20190513102834', 'checkdate': '104753.837000', 'hospital': 'TianJin HuanHu Hospital-GX'}
#{'PatientName': 'DING_ZHI_XIA', 'sex': 'F', 'borndate': '19630510', 'fid': 'F0334166', 'Study_id': 'MR20171231111726', 'checkdate': '113155.885000', 'hospital': 'TianJin HuanHu Hospital'}

TCL_add("DING_ZHI_XIA","F","MR20171231111726","TianJin HuanHu Hospital",'19630510')